<template>
  <div class="host">
    <h1>主机管理</h1>
    <el-button type="primary" @click="showAddHostDialog" style="margin-bottom: 20px;">添加主机</el-button>
    <el-table :data="hostStore.hostData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="hostName" label="主机名" width="180" />
      <el-table-column prop="hostIp" label="IP地址" width="180" />
      <el-table-column prop="username" label="主机账号" width="150" />
      <el-table-column prop="port" label="端口号" width="100" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '运行中' : '已停止' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="addHostVisible" title="添加主机" width="30%">
      <el-form :model="newHost" label-width="120px">
        <el-form-item label="主机名称">
          <el-input v-model="newHost.hostName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="主机IP">
          <el-input v-model="newHost.hostIp" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="端口号">
          <el-input v-model="newHost.port" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="newHost.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="newHost.password" type="password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addHostVisible = false">取消</el-button>
          <el-button type="primary" @click="addHost">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useHostStore } from '../stores/host'
import { deleteHostInfo } from '../api/host'


// 获取 Pinia store 实例
const hostStore = useHostStore()

// 页面加载时调用 API 获取主机数据
onMounted(() => {
  hostStore.loadHostData()  // 从 Pinia store 获取主机数据
})


const addHostVisible = ref(false)
const newHost = ref({
  hostName: '',
  hostIp: '',
  username: '',
  password: '',
  port: ''
})

const showAddHostDialog = () => {
  addHostVisible.value = true
}

const addHost = async () => {
  try {
    await hostStore.addHost(newHost.value)  // 调用 Pinia store 中的 addHost 方法
    ElMessage.success(`主机 ${newHost.value.hostIp} 添加成功！`)
    addHostVisible.value = false

    // 添加主机后，重新加载列表
    hostStore.loadHostData()
  } catch (error) {
    console.error(error)
    ElMessage.error('添加主机失败，请重试！')
  }
}

const handleEdit = (index, row) => {
  console.log(index, row)
  // 这里添加编辑逻辑
}

const handleDelete = (index, row) => {
  deleteHostInfo(row.id);
  // 添加主机后，重新加载列表
  hostStore.loadHostData()
}
</script>

<style scoped>
.host {
  padding: 20px;
}
</style>
